package com.crm.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;

public class MongoTest {
	public static void main(String[] args) {
		try {  
            ServerAddress serverAddress = new ServerAddress("60.205.136.122",19527);  
            List<ServerAddress> addrs = new ArrayList<ServerAddress>();  
            addrs.add(serverAddress);  
            MongoCredential credential = MongoCredential.createScramSha1Credential("zhcrm", "zhcrm", "zhcrm".toCharArray());  
            List<MongoCredential> credentials = new ArrayList<MongoCredential>();  
            credentials.add(credential);  
              
            //通过连接认证获取MongoDB连接  
            MongoClient mongoClient = new MongoClient(addrs,credentials);  
              
            //连接到数据库  
            MongoDatabase mongoDatabase = mongoClient.getDatabase("zhcrm");  
            
            BasicDBObject query1 = new BasicDBObject();
    	    
    	    query1.put("createDate", BasicDBObjectBuilder.start("$gte", new Date(2016-1900 , 12 - 1, 1)).add("$lt", new Date(2016 - 1900, 12-1 , 3)).get());
            
            ;
            System.out.println(JsonUtil.toJsonStr(mongoDatabase.getCollection("countWorkBench").find(query1)));  
        } catch (Exception e) {  
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );  
        }  
    }  
}
